package com.xfhy.createpicture

import android.Manifest
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.support.v4.content.FileProvider
import android.view.PixelCopy
import android.widget.Toast
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.io.FileOutputStream
import java.lang.Exception
import android.content.Intent
import android.net.Uri


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initPermission()
        initView()
    }

    private fun initView() {
        btn_create_pic.setOnClickListener {
            val color = java.util.Random().nextInt(10)
            val finalColor: Int = when (color) {
                0 -> {
                    0xFFef9a9a.toInt()
                }
                1 -> {
                    0xFF90CAF9.toInt()
                }
                2 -> {
                    0xFF80CBC4.toInt()
                }
                3 -> {
                    0xFFE6EE9C.toInt()
                }
                4 -> {
                    0xFFFFAB91.toInt()
                }
                5 -> {
                    0xFFB0BEC5.toInt()
                }
                6 -> {
                    0xFFBA68C8.toInt()
                }
                7 -> {
                    0xFF7986CB.toInt()
                }
                8 -> {
                    0xFF4FC3F7.toInt()
                }
                9 -> {
                    0xFF4FC3F7.toInt()
                }
                else -> {
                    0xFFAED581.toInt()
                }
            }
            val colorDrawable = ColorDrawable(finalColor)
            iv_color.setImageDrawable(colorDrawable)

            savePic()
        }
    }

    @SuppressLint("CheckResult")
    private fun initPermission() {
        val rxPermissions = RxPermissions(this)
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe { granted ->
            if (granted) {
                btn_create_pic.isEnabled = true
            } else {
                finish()
            }
        }
    }

    private fun savePic() {
        val bitmap = ImageUtil.drawable2Bitmap(iv_color.drawable)
        try {
            //创建文件夹
            val dir = "${Environment.getExternalStorageDirectory()}/savepic/"
            val fileDir = File(dir)
            if (!fileDir.exists()) {
                fileDir.mkdirs()
            }

            //创建文件
            val path = "${Environment.getExternalStorageDirectory()}/savepic/${System.currentTimeMillis()}.jpg"
            val file = File(path)
            file.createNewFile()
            val fos = FileOutputStream(file)
            //保存
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos)
            bitmap.recycle()


            Toast.makeText(this, "保存图片成功!", Toast.LENGTH_LONG).show()

            //保存图片后发送广播通知更新数据库
            val uri = Uri.fromFile(file)
            sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri))
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(this, "保存图片失败!", Toast.LENGTH_LONG).show()
        }
    }

}
